﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;


namespace LI4
{
    public partial class MenuPrincipal : Form
    {
        public MenuPrincipal()
        {
            InitializeComponent();
            pesquisaT.Enabled = false;
           
            

        }

        private void cursosToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void novoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
        }

        private void novoToolStripMenuItem2_Click(object sender, EventArgs e)
        {
            new NovoCursoF().Show();
        }

        private void editarToolStripMenuItem2_Click(object sender, EventArgs e)
        {
            new EditarCF(null).Show();
        }

        private void MenuPrincipal_Load(object sender, EventArgs e)
        {
            if (Program.Login.Tipo == Business.Login.OPERADOR) administracaoM.Dispose();
        }

        private void utilizadorToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new NovoUtilizador().Show();
        }

        private void empresaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new Presentation.NovaEmpresa().Show();
        }

        private void areaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new Presentation.NovoOperador().Show();
        }

        

        private void utilizadorToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            new EditarUtilizador(null).Show();
        }

        private void empresaToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            new Presentation.EditarEmpresa(null).Show();
        }

        private void inscreverCandidatoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new Presentation.InscreverCF().Show();
        }

        private void novoToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            new NovoAnuncio().Show();
        }

        private void editarToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            new Presentation.EscolherAnuncio().Show();
        }

        private void operadorToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new Presentation.EditarOperador(null).Show();
        }

        private void areaToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            new Presentation.NovaArea().Show();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string descricao = "Logout do sistema: " + Program.Login.Username + ".";
            Data.Registos regData = new LI4.Data.Registos();
            Business.Registo registo = new Business.Registo(Program.Login.Username, descricao, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            regData.inserir(registo);
            Application.Exit();
        }

        private void MenuPrincipal_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        private void registosDeSistemaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new Presentation.MenuRegistos().Show();
        }

        private void importarBaseDeDadosToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            openFileDialog1.InitialDirectory = path;
            openFileDialog1.FileName = "";
            openFileDialog1.Filter = "(*xml)|*xml";

            openFileDialog1.ReadOnlyChecked = true;
            openFileDialog1.ShowReadOnly = true;

            string filename = "";
            string path1 = "";
            string fullname = "";

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                filename = System.IO.Path.GetFileName(openFileDialog1.FileName);
                path = System.IO.Path.GetDirectoryName(openFileDialog1.FileName);
                fullname = String.Concat(path1, filename);
                System.IO.Directory.Move(fullname, path1 + "NOVOFICHEIRO.pdf");
            }
            //RESET

            openFileDialog1.RestoreDirectory = true;
            openFileDialog1.Dispose();
        }

        

        private void estatisticasToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new Presentation.MenuEstatisticas().Show();
        }

        private void procurarB_Click(object sender, EventArgs e)
        {
            
            pesquisaT.Rows.Clear();
            string palavra = procuraTB.Text.Trim();

            Data.Login dataPro = new LI4.Data.Login();

            ArrayList pesquisa = dataPro.pesquisa(palavra);

            if (pesquisa.Count > 0)
            {
                pesquisaT.Enabled = true;
                pesquisaT.Enabled = true;

                string[] row = new string[4];

                foreach (string[] reg in pesquisa)
                {
                    row[1] = reg[0];
                    row[2] = reg[1];
                    row[3] = reg[2];
                    pesquisaT.Rows.Add(row);
                }
            }
            else
            {
                procuraTB.Text = "";
                pesquisaT.Enabled = false;
                MessageBox.Show(this, "Não foi encontrado nenhum resultado.\nTente novamente!", "Pesquisa.", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }


            
        }

        private void pesquisaT_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0 || e.ColumnIndex !=
            pesquisaT.Columns["butaoCOL"].Index) return;

            string tipo=null;

            try
            {
                tipo = pesquisaT["tipoCOL", e.RowIndex].Value.ToString();
            }
            catch { return; }
            //MessageBox.Show(this, tipo, "Erro.", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

            if (tipo.Equals("Candidato"))
            {
                Data.Candidato dataC = new LI4.Data.Candidato();

                string username = pesquisaT["idCOL", e.RowIndex].Value.ToString();

                Business.Candidato cand = dataC.getCandidato(username);

                new EditarUtilizador(cand).Show();

            }
            if (tipo.Equals("Empresa"))
            {
                Data.Empresa dataE = new LI4.Data.Empresa();

                string username = pesquisaT["idCOL", e.RowIndex].Value.ToString();

                Business.Empresa empr = dataE.getEmpresa(username);

                new Presentation.EditarEmpresa(empr).Show();

            }
            if (tipo.Equals("Anúncio"))
            {
                Data.Anuncio dataA = new LI4.Data.Anuncio();

                try
                {
                    int cod_anuncio = Int32.Parse(pesquisaT["idCOL", e.RowIndex].Value.ToString());

                    Business.Anuncio anuncio = dataA.getAnuncio(cod_anuncio);
                    Business.Utilizador util = getUtilAnuncio(anuncio.Username);

                    if(util!= null) new EditarAnuncio(anuncio,util).Show();
                }
                catch { }

            }
            if (tipo.Equals("Curso"))
            {
                Data.Curso dataC = new LI4.Data.Curso();

                try
                {
                    int cod_curso = Int32.Parse(pesquisaT["idCOL", e.RowIndex].Value.ToString());

                    Business.Curso curso = dataC.getCurso(cod_curso);
                    
                    if (curso != null) new EditarCF(curso).Show();
                }
                catch { }
            }
            if (tipo.Equals("Registo"))
            {
                try
                {
                    string username = pesquisaT["idCOL", e.RowIndex].Value.ToString();
                    string descricao = pesquisaT["descricaoCOL", e.RowIndex].Value.ToString();

                    char[] splitter  = {','};

                    string[] descData = descricao.Split(splitter,2);
                    pesquisaT["descricaoCOL", e.RowIndex].ToolTipText = descricao; ;
                    string registo = "Username: " + username + "\nDescricao: "+descData[0]+"\nData: "+descData[1];
                    MessageBox.Show(this, registo, "Registo.", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                catch { }

            }
            if (tipo.Equals("Operador"))
            {
                Data.Operador dataOp = new LI4.Data.Operador();
                if (Program.Login.Tipo == Business.Login.SUPERIOR)
                {
                    try
                    {
                        string username = pesquisaT["idCOL", e.RowIndex].Value.ToString();

                        Business.Operador op = dataOp.getOperador(username);

                        new Presentation.EditarOperador(op).Show();

                    }
                    catch { }
                }
                else
                    MessageBox.Show(this, "Não tem permissão!", "Acesso.", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

            }

           
        }

        private Business.Utilizador getUtilAnuncio(string username)
        {
            Business.Utilizador util=null;
            Data.Anuncio anunciosData = new LI4.Data.Anuncio();

            Data.Candidato candData = new LI4.Data.Candidato();
            Data.Empresa empData = new LI4.Data.Empresa();
            int tipo;



            if (candData.exiteCandidato(username))
            {
                tipo = Business.Login.CANDIDATO;
            }
            else
            {
                if (empData.existeEmpresa(username)) tipo = Business.Login.EMPRESA;
                else
                {
                    MessageBox.Show(this, "Username nao existente!", "Erro.", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    tipo = 0;
                }
            }
            if (tipo != 0)
            {
                if (tipo == Business.Login.CANDIDATO)
                {
                    util = new LI4.Business.Candidato(candData.getCandidato(username));
                }
                else
                {
                    util = new LI4.Business.Empresa(empData.getEmpresa(username));
                }
            } 
            else
                MessageBox.Show(this, "Utilizador nao tem anúncios!", "Erro.", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            tipo = 0;

            return util;
        }

        private void MenuPrincipal_FormClosing(object sender, FormClosingEventArgs e)
        {
            string descricao = "Logout no sistema: " + Program.Login.Username + ".";
            Data.Registos regData = new LI4.Data.Registos();
            Business.Registo registo = new Business.Registo(Program.Login.Username, descricao, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            regData.inserir(registo);
        }

        private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void sobreToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new Presentation.Sobre().Show();
        }
    }
}
